package com.rong.DAO;

import com.opensymphony.xwork2.ActionContext;
import com.rong.hibernate.BaseHibernateDAO;
import com.rong.hibernate.Const;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.rong.ORM.ZynCash;
import com.rong.ORM.ZynPrice2;
import com.rong.ORM.ZynQueryPrice;
import com.rong.ORM.ZynUser;

/**
 * A data access object (DAO) providing persistence and search support for
 * ZynCash entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.rong.DAO.ZynCash
 * @author MyEclipse Persistence Tools
 */

public class ZynCashDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(ZynCashDAO.class);
	// property constants
	public static final String COM_ID = "comId";
	public static final String SUM = "sum";
	public static final String REMARK = "remark";
	public static final String SUB_COM_ID = "subComId";
	public static final String BF_NEXT_SUM = "bfNextSum";
	public static final String LT_PRE_SUM = "ltPreSum";
	public static final String EXTATTR1INT = "extattr1int";
	public static final String EXTATTR2INT = "extattr2int";
	public static final String EXTATTR3INT = "extattr3int";
	public static final String EXTATTR1DB = "extattr1db";
	public static final String EXTATTR2DB = "extattr2db";
	public static final String EXTATTR3DB = "extattr3db";
	public static final String EXTATTR4DB = "extattr4db";
	public static final String EXTATTR1 = "extattr1";
	public static final String EXTATTR2 = "extattr2";
	public static final String EXTATTR3 = "extattr3";
	public static final String EXTATTR4 = "extattr4";
	public static final String EXTATTR5 = "extattr5";
	public static final String EXTATTR6 = "extattr6";
	public static final String FACT_SUM = "factSum";

	public void save(ZynCash transientInstance) {
		log.debug("saving ZynCash instance");
		try {
			 Transaction ts = null;
             ts = getSession().beginTransaction();
			getSession().save(transientInstance);
			ts.commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(ZynCash persistentInstance) {
		log.debug("deleting ZynCash instance");
		try {
			 Transaction ts = null;
             ts = getSession().beginTransaction();
			getSession().delete(persistentInstance);
			ts.commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public ZynCash findById(java.lang.Integer id) {
		log.debug("getting ZynCash instance with id: " + id);
		try {
			ZynCash instance = (ZynCash) getSession().get(ZynCash.class, id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(ZynCash instance) {
		log.debug("finding ZynCash instance by example");
		try {
			List results = getSession().createCriteria("com.rong.DAO.ZynCash")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding ZynCash instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from ZynCash as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByComId(Object comId) {
		return findByProperty(COM_ID, comId);
	}

	public List findBySum(Object sum) {
		return findByProperty(SUM, sum);
	}

	public List findByRemark(Object remark) {
		return findByProperty(REMARK, remark);
	}

	public List findBySubComId(Object subComId) {
		return findByProperty(SUB_COM_ID, subComId);
	}

	public List findByBfNextSum(Object bfNextSum) {
		return findByProperty(BF_NEXT_SUM, bfNextSum);
	}

	public List findByLtPreSum(Object ltPreSum) {
		return findByProperty(LT_PRE_SUM, ltPreSum);
	}

	public List findByExtattr1int(Object extattr1int) {
		return findByProperty(EXTATTR1INT, extattr1int);
	}

	public List findByExtattr2int(Object extattr2int) {
		return findByProperty(EXTATTR2INT, extattr2int);
	}

	public List findByExtattr3int(Object extattr3int) {
		return findByProperty(EXTATTR3INT, extattr3int);
	}

	public List findByExtattr1db(Object extattr1db) {
		return findByProperty(EXTATTR1DB, extattr1db);
	}

	public List findByExtattr2db(Object extattr2db) {
		return findByProperty(EXTATTR2DB, extattr2db);
	}

	public List findByExtattr3db(Object extattr3db) {
		return findByProperty(EXTATTR3DB, extattr3db);
	}

	public List findByExtattr4db(Object extattr4db) {
		return findByProperty(EXTATTR4DB, extattr4db);
	}

	public List findByExtattr1(Object extattr1) {
		return findByProperty(EXTATTR1, extattr1);
	}

	public List findByExtattr2(Object extattr2) {
		return findByProperty(EXTATTR2, extattr2);
	}

	public List findByExtattr3(Object extattr3) {
		return findByProperty(EXTATTR3, extattr3);
	}

	public List findByExtattr4(Object extattr4) {
		return findByProperty(EXTATTR4, extattr4);
	}

	public List findByExtattr5(Object extattr5) {
		return findByProperty(EXTATTR5, extattr5);
	}

	public List findByExtattr6(Object extattr6) {
		return findByProperty(EXTATTR6, extattr6);
	}

	public List findByFactSum(Object factSum) {
		return findByProperty(FACT_SUM, factSum);
	}

	public List findAll() {
		log.debug("finding all ZynCash instances");
		try {
			String queryString = "from ZynCash";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public ZynCash merge(ZynCash detachedInstance) {
		log.debug("merging ZynCash instance");
		try {
			 Transaction ts = null;
             ts = getSession().beginTransaction();
			ZynCash result = (ZynCash) getSession().merge(detachedInstance);
			ts.commit();
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(ZynCash instance) {
		log.debug("attaching dirty ZynCash instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(ZynCash instance) {
		log.debug("attaching clean ZynCash instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	/**
	 * 查找并返回符合条件的价格记录
	 */

	public List<ZynCash> findCashQueryByPage(int page, int rowsPerPage,ZynQueryPrice queryCrt) {
		
		ActionContext actionContext = ActionContext.getContext();         
	     Map session = actionContext.getSession(); 
	     ZynUser loginUser=(ZynUser)session.get("loginUser");   //登录用户	
	
		Transaction ts2=null;
		ts2=getSession().beginTransaction();
		getSession().clear();
				
		Criteria crt = getSession().createCriteria(ZynCash.class);
		  Criteria crtCom = crt.createCriteria("comObj");
		crt.addOrder(org.hibernate.criterion.Order.desc("id"));
		
		if(queryCrt.getStartDate()!=null){
			crt.add(Restrictions.ge("cashDate", queryCrt.getStartDate()));	
		}
		
		if(queryCrt.getFromComName()!=null&&!queryCrt.getFromComName().equalsIgnoreCase("")){ //公司
			
			crtCom.add(Restrictions.like("comName", queryCrt.getFromComName(),MatchMode.ANYWHERE));
		}
	
		if(queryCrt.getPrice()!=null&&queryCrt.getPrice()>0){ //现金金额
			crt.add(Restrictions.ge("csum", queryCrt.getPrice()));	
		}
				
		//--权限判断,分公司只能看本公司单据，本部可以查看全部		
		if(loginUser.getZyncom().getComProperty().equalsIgnoreCase(Const.comTypetop)){			
			
		}else{			
					//所属分公司
			crt.add(Restrictions.eq("subComId", loginUser.getComId()));	
			
			  //匹配单据的所属公司
		}			
		crt.setMaxResults(rowsPerPage); // 每页最多显示几条
		crt.setFirstResult((page - 1) * rowsPerPage); // 每页从第几条记录开始
		ts2.commit();
		List<ZynCash> list = crt.list();
		for (int i = 0; i < list.size(); i++) {
			System.out.println("==========cash list by page======"+list.get(i).getId());
			
		}
 
		return list;
	}
	/**
	 * 查找并返回所有价格记录数
	 */

	public List<ZynCash> cashTatolRows(ZynQueryPrice queryCrt) {
		getSession().clear();

		ActionContext actionContext = ActionContext.getContext();         
	     Map session = actionContext.getSession(); 
	     ZynUser loginUser=(ZynUser)session.get("loginUser");   //登录用户	
	     Criteria crt = getSession().createCriteria(ZynCash.class);
	     Criteria crtCom = crt.createCriteria("comObj");
	   	crt.addOrder(org.hibernate.criterion.Order.desc("id"));
	   	if(queryCrt.getStartDate()!=null){
			crt.add(Restrictions.ge("cashDate", queryCrt.getStartDate()));	
		}
		
		if(queryCrt.getFromComName()!=null&&!queryCrt.getFromComName().equalsIgnoreCase("")){ //公司
			
			crtCom.add(Restrictions.like("comName", queryCrt.getFromComName(),MatchMode.ANYWHERE));
		}
	
		if(queryCrt.getPrice()!=null&&queryCrt.getPrice()>0){ //现金金额
			crt.add(Restrictions.ge("csum", queryCrt.getPrice()));	
		}
				
		//--权限判断,分公司只能看本公司单据，本部可以查看全部		
		if(loginUser.getZyncom().getComProperty().equalsIgnoreCase(Const.comTypetop)){			
			
		}else{			
					//所属分公司
			crt.add(Restrictions.eq("subComId", loginUser.getComId()));	
			
			  //匹配单据的所属公司
		}			
		List<ZynCash> list = crt.list();
		return list;
	}
	
}